/**
 * Copyright (c) 2007 Samsung Networks, Inc. All Rights Reserved.
 */
package batch.dcms;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import batch.common.db.ConnectionManager;
import batch.common.report.LogWriter;

import java.io.FileOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * Copyright (c) 2007 Samsung Networks, Inc. All Rights Reserved.
 *
 * Class Name  : DcmsKazBatch.java
 * Description : 카자흐스탄 DCMS 일배치 
 *
 * Mod Date    : 2009. 11. 27
 * Modifier    : jglee2
 * Description : Create
 *
 * @author: jglee2
 * @version: 1.0
 * @since: 1.0
 */
public class DcmsKazBatch {

	private final String TODAY       = DateFormatUtils.format(new Date(), "yyyyMMdd");  //Get Current Date
    private String sMethodNM = "start";
    private StringBuffer sql = new StringBuffer();
    
    String phoneXML = "http://webservice.i-free.ru/ExportContent.soap/LoadPhones?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}";
    String operatorXML = "http://webservice.i-free.ru/ExportContent.soap/LoadOperators?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}";
    
    String[] TYPE = {"COLOR_IMAGE", "ANIMATION", "VIDEO", "RINGTONE_POLY", "REALTONE", "JAVA"};
    
    String[] XML_URL = {
		  "http://webservice.i-free.ru/ExportContent.soap/LoadContent?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}&ProductType=COLOR_IMAGE&dateStart="
		, "http://webservice.i-free.ru/ExportContent.soap/LoadContent?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}&ProductType=ANIMATION&dateStart="
    	, "http://webservice.i-free.ru/ExportContent.soap/LoadContent?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}&ProductType=VIDEO&dateStart="
    	, "http://webservice.i-free.ru/ExportContent.soap/LoadContent?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}&ProductType=RINGTONE_POLY&dateStart="
    	, "http://webservice.i-free.ru/ExportContent.soap/LoadContent?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}&ProductType=REALTONE&dateStart="
    	, "http://webservice.i-free.ru/ExportContent.soap/LoadContent?ProjectGUID={05912fd2-a9c8-4181-8746-99c2822fba6f}&ProductType=JAVA&dateStart="
    };
    boolean isDebug= false;
    
    String LANG_CD = "RUS";
    String[] LANG_CDS = {"RUS", "KAZ"};
    //String LANG_CD = "KAZ";
    String SITE_CD = "kaz";
    NodeList Categories;
    NodeList Products;
    
    NodeList Phones;
    NodeList Vendors;
    NodeList Operators;

	public DcmsKazBatch()
	{
		isDebug = true;
	}

	String NVL(String str)
	{
		if(str==null)
			return "";

		return str;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		DcmsKazBatch a = new DcmsKazBatch();
		a.execute();

	}
	
	public boolean writeXmlFile(Document doc, String fileName)
	{
		try{
	        Transformer t = TransformerFactory.newInstance().newTransformer(); 
			FileOutputStream fw = new FileOutputStream("../xml/" + fileName);
			t.transform(new DOMSource(doc), new StreamResult(fw));
			fw.close();
        }
        catch(Exception e)
        {
        	LogWriter.setLog("############# write file fail #################");
        	LogWriter.setLog(e.getMessage());
        	LogWriter.setLog("############# //write file fail #################");
        }
        finally
        {
        	
        }
        return true;
	}
	
	public boolean execute()
	{
		Connection conn = null;
		try{
			System.out.println("## kaz start ##");
			LogWriter.setFistStart("DcmsKazBatch.class", "java DcmsKazBatch");
			conn = ConnectionManager.getConnection();
			conn.setAutoCommit(false);
			readPhoneXML();
			readOperatorXML();
			for(int h=0;h<LANG_CDS.length;h++)
			{
				//KAZ, RUS 국가에 대해서 적용
				LANG_CD = LANG_CDS[h];
				System.out.println("################ " + LANG_CD + " start ###############");
				LogWriter.setLog("################ " + LANG_CD + " start ###############");
				deleteProduct(conn);
				executePhone(conn, this.Phones);
				executeVendor(conn, this.Vendors);
				executeOperator(conn, this.Operators);
				for(int i=0;i<TYPE.length;i++)
				{
					readProductXML(i);
					String productType = TYPE[i];
					System.out.println("######## : " + productType);
					executeCategories(conn, productType, this.Categories);
			        executeProducts(conn, productType, this.Products);
			        deletePopularPrd(conn, productType);
			        
				}
				conn.commit();
				System.out.println("################ " + LANG_CD + " end ###############");
				LogWriter.setLog("################ " + LANG_CD + " end ###############");
			}
			
			
			LogWriter.setLog("################ batch success ###############");
			System.out.println("## success ##");
		}
		catch(Exception e)
		{
			LogWriter.setLog("################ batch fail ###############");
			LogWriter.setLog(e.getLocalizedMessage());
			System.out.println("## fail ##");
			e.printStackTrace();
			
			 if (conn != null) {
                 try {
                     conn.rollback();
                 } catch (Exception se) {}
             }
		}
		finally
		{
			if (conn != null) {
                try {
                    conn.close();
                    conn = null;
                } catch (Exception se) {
                	LogWriter.setLog(se.getMessage());
                	se.printStackTrace();
                }
            }
		}

		LogWriter.setEnd();	
		System.out.println("## kaz end ##");
        return true;
	}
	public boolean readPhoneXML()
	{
		DocumentBuilderFactory fact = null;
		DocumentBuilder builder = null;
		Document doc= null;
		
		try{
			this.Vendors = null;
			this.Phones = null;
			
			fact = DocumentBuilderFactory.newInstance();
			builder = fact.newDocumentBuilder();

			doc = builder.parse(this.phoneXML);
			String fileName = SITE_CD + "_" + LANG_CD + "_" + "LoadPhones_" + TODAY + ".xml"; 
			writeXmlFile(doc, fileName);
			this.Vendors = doc.getElementsByTagName("Vendor");
			this.Phones = doc.getElementsByTagName("Phone");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			doc = null;
			builder = null;
			fact = null;
		}
		return true;
	}
	
	public boolean readOperatorXML() throws Exception
	{
		DocumentBuilderFactory fact = null;
		DocumentBuilder builder = null;
		Document doc= null;
		try{
			fact = DocumentBuilderFactory.newInstance();
			builder = fact.newDocumentBuilder();
			doc = builder.parse(this.operatorXML);
			String fileName = SITE_CD + "_" + LANG_CD + "_" + "LoadOperators_" + TODAY + ".xml";
			writeXmlFile(doc, fileName);
			this.Operators = doc.getElementsByTagName("Operator");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		finally
		{
			doc = null;
			builder = null;
			fact = null;
		}

		return true;
	}
	
	public boolean readProductXML(int idx) throws Exception
	{
		DocumentBuilderFactory fact = null;
		DocumentBuilder builder = null;
		Document doc= null;
		
		try{
			this.Categories = null;
			this.Products = null;
			
			String xmlUrl = XML_URL[idx];
			fact = DocumentBuilderFactory.newInstance();
			builder = fact.newDocumentBuilder();
			doc = builder.parse(xmlUrl);
			String fileName = SITE_CD + "_" + LANG_CD + "_" + TYPE[idx] + "_" + TODAY + ".xml";
			writeXmlFile(doc, fileName);
		
			this.Categories = doc.getElementsByTagName("Category");
			
			if(idx==5)
				this.Products = doc.getElementsByTagName("JavaProduct");
			else
				this.Products = doc.getElementsByTagName("Product");
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		finally
		{
			doc = null;
			builder = null;
			fact = null;
		}
		return true;
	}

	private boolean deleteProduct(Connection conn) throws Exception
	{
		String sql = "";
		Statement stmt = null;
		try{
			stmt = conn.createStatement();
			sql = "delete from G_SFC_DCMS_PRODUCT where site_cd = '" + SITE_CD + "' and lang_cd = '" + LANG_CD + "'";
			stmt.addBatch(sql);
			
	        sql = "delete from G_SFC_DCMS_CATE_INFO where site_cd = '" + SITE_CD + "' and lang_cd = '" + LANG_CD + "'";
	        stmt.addBatch(sql);
	        
	        sql = "delete from G_SFC_DCMS_CATEGORY where site_cd = '" + SITE_CD + "' and lang_cd = '" + LANG_CD + "'";
	        stmt.addBatch(sql);
	        
	        sql = "delete from G_SFC_DCMS_JAVA_SUPPORTED where site_cd = '" + SITE_CD + "' and lang_cd = '" + LANG_CD + "'";
	        stmt.addBatch(sql);
	        
	        sql = "delete from G_SFC_DCMS_PHONE where site_cd = '" + SITE_CD + "' and lang_cd = '" + LANG_CD + "'";
	        stmt.addBatch(sql);
	        
	        sql = "delete from G_SFC_DCMS_VENDOR where site_cd = '" + SITE_CD + "' and lang_cd = '" + LANG_CD + "'";
	        stmt.addBatch(sql);
	        
	        sql = "delete from G_SFC_DCMS_OPERATOR where site_cd = '" + SITE_CD + "' and lang_cd = '" + LANG_CD + "'";
	        stmt.addBatch(sql);
	        
	        stmt.executeBatch();
		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (stmt != null) try { stmt.close(); } catch (Exception e) { e.printStackTrace(); throw e;}
        }
		return true;
	}
	
	public boolean executePhone(Connection conn, NodeList Phones) throws Exception
	{
		PreparedStatement pstmt = null;
		String sql = "insert into G_SFC_DCMS_PHONE(SITE_CD, LANG_CD, PHONE_ID, PHONE_NAME, VENDOR_NAME, VENDOR_ID, USER_AGENT, CREATE_DATE) values(";
		try{
			sql += "?, ?, ?, ?, ?, ?, ?, sysdate)";

	        
			pstmt = conn.prepareStatement(sql.toString());

			for(int i=0;i<Phones.getLength();i++)
			{
				Node cur = Phones.item(i);
				NamedNodeMap attrs =  cur.getAttributes();

				Node idNode = attrs.getNamedItem("ID");
				if(idNode != null)
				{
		            pstmt.setString( 1, SITE_CD);  //SITE_CD
		            pstmt.setString( 2, LANG_CD);  //LANG_CD
		            pstmt.setString( 3, NVL(idNode.getNodeValue()));  //CATE_ID
		            
		            Node child = cur.getFirstChild();
		            while(child!=null)
		            {
		            	if("Name".equals(child.getNodeName())){
		            		pstmt.setString( 4, child.getTextContent() );  //PHONE_NAME
		            	}else if("Vendor".equals(child.getNodeName())){
		            		pstmt.setString( 5, child.getTextContent() );  //PHONE_NAME
		            	}else if("VendorID".equals(child.getNodeName())){
		            		pstmt.setString( 6, child.getTextContent() );  //PHONE_NAME
		            	}else if("UserAgent".equals(child.getNodeName())){
		            		pstmt.setString( 7, child.getTextContent() );  //PHONE_NAME
		            	}
		            			
		            	child = child.getNextSibling();
		            }
	
		            pstmt.addBatch();
				}
			}
			
			pstmt.executeBatch();

		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e;}
        }
		return true;
	}
	public boolean executeVendor(Connection conn, NodeList Vendors) throws Exception
	{
		PreparedStatement pstmt = null;
		String sql = "insert into G_SFC_DCMS_VENDOR(SITE_CD, LANG_CD, VENDOR_ID, VENDOR_NAME, IMG_VENDOR, ALT_VENDOR, CREATE_DATE) values(";
		try{
			sql += "?, ?, ?, ?, ?, ?, sysdate)";
	        
			pstmt = conn.prepareStatement(sql.toString());

			for(int i=0;i<Vendors.getLength();i++)
			{
				Node cur = Vendors.item(i);
				NamedNodeMap attrs =  cur.getAttributes();

				Node idNode = attrs.getNamedItem("ID");
				if(idNode != null)
				{
		            pstmt.setString( 1, SITE_CD);  //SITE_CD
		            pstmt.setString( 2, LANG_CD);  //LANG_CD
		            pstmt.setString( 3, NVL(idNode.getNodeValue()));  //CATE_ID
		            
		            Node child = cur.getFirstChild();
		            while(child!=null)
		            {
		            	if("Name".equals(child.getNodeName())){
		            		pstmt.setString( 4, child.getTextContent() );  //PHONE_NAME
		            	}else if("Image".equals(child.getNodeName())){
		            		String imgName = child.getTextContent();
		            		pstmt.setString( 5,  imgName);  //PHONE_NAME
		            	}
		            	
		            	child = child.getNextSibling();
		            }
		            pstmt.setString( 6, "");  //CATE_ID
		            pstmt.addBatch();
				}
			}
			pstmt.executeBatch();
		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e;}
        }
		return true;
	}
	public boolean executeOperator(Connection conn, NodeList Operators) throws Exception
	{
		PreparedStatement pstmt = null;
		String sql = "insert into G_SFC_DCMS_OPERATOR(SITE_CD, LANG_CD, OPERATOR_ID, OPERATOR_NAME, IMG_OPERATOR, ALT_OPERATOR, COUNTRY, CREATE_DATE) values(";
			   sql += "?, ?, ?, ?, ?, ?, ?, sysdate)";
        
		try{
			
			pstmt = conn.prepareStatement(sql.toString());

			for(int i=0;i<Operators.getLength();i++)
			{
				Node cur = Operators.item(i);
				NamedNodeMap attrs =  cur.getAttributes();

				Node idNode = attrs.getNamedItem("ID");
				Node nameNode = attrs.getNamedItem("Name");
				Node imgNode = attrs.getNamedItem("ImageUrl");
				Node countryNode = attrs.getNamedItem("Country");
				String imgName = NVL(imgNode.getNodeValue());
				if(imgName.length()>0)
					imgName = imgName.substring(imgName.lastIndexOf("/") + 1, imgName.length());
				
				if(idNode != null)
				{
		            pstmt.setString( 1, SITE_CD);  //SITE_CD
		            pstmt.setString( 2, LANG_CD);  //LANG_CD
		            pstmt.setString( 3, NVL(idNode.getNodeValue()));  //id
		            pstmt.setString( 4, NVL(nameNode.getNodeValue()));  //name
		            pstmt.setString( 5, imgName);  //img
		            pstmt.setString( 6, "");  //alt
		            pstmt.setString( 7, NVL(countryNode.getNodeValue()));  //country
		            pstmt.addBatch();
				}
			}
			pstmt.executeBatch();
		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e; }
        }
		return true;
	}


	public String executeCategories(Connection conn, String productType, NodeList Categories) throws Exception
	{
		PreparedStatement pstmt = null;
		String sql = "insert into G_SFC_DCMS_CATEGORY(SITE_CD, LANG_CD, CATE_ID, CATE_NAME, CREATE_DATE, TYPE) values(";
		try{
			sql += "?, ?, ?, ?, sysdate, ?)";

	        
			pstmt = conn.prepareStatement(sql.toString());

			for(int i=0;i<Categories.getLength();i++)
			{
				Node cur = Categories.item(i);
				NamedNodeMap attrs =  cur.getAttributes();
				/*
				System.out.println("<" +cur.getNodeName() + ">" + NVL(cur.getNodeValue()));
				System.out.println("<attrs>");
				for(int j=0;j<attrs.getLength();j++)
				{
					Node attr = attrs.item(j);
					System.out.println("   [" +attr.getNodeName() + "]" + NVL(attr.getNodeValue()));
				}
				System.out.println("</attrs>");
				System.out.println("</" +cur.getNodeName() + ">");
				*/

				Node nameNode = attrs.getNamedItem("Name");
				Node idNode = attrs.getNamedItem("ID");
				if(nameNode!=null && idNode != null)
				{
		            pstmt.setString( 1, SITE_CD);  //SITE_CD
		            pstmt.setString( 2, LANG_CD);  //LANG_CD
		            pstmt.setString( 3, NVL(idNode.getNodeValue()));  //CATE_ID
		            pstmt.setString( 4, NVL(nameNode.getNodeValue()));  //CATE_NAME
		            pstmt.setString( 5, productType);  //TYPE
	
		            pstmt.addBatch();
				}
			}
			pstmt.executeBatch();

		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e; }
        }
		return sql;
	}
	
	public void printDebug(String sql, PreparedStatement pstmt)
	{
		try{
			if(isDebug){
				
	            System.out.println("===========================================================");
	            System.out.println("[SQL   ]" + sql);
	            System.out.print("[PARAMS][");
	            System.out.println("]");
	            System.out.println("===========================================================");
	        }
		}
		catch(Exception e)
		{
			
		}
	}
	
	public String executeProducts(Connection conn, String productType, NodeList Products) throws Exception
	{
		PreparedStatement pstmt = null;
		int cnt=0;
		String sql 	= "insert into G_SFC_DCMS_PRODUCT(SITE_CD, LANG_CD, PRODUCT_ID, PRODUCT_NAME, PRODUCT_DESC, PRODUCT_TYPE, VENDOR";
		
		if("RINGTONE_POLY".equals(productType) || "REALTONE".equals(productType))
		{
			sql +=", CREATE_DATE, RATING, ORDER_NO, FILE1, FILE_DESC1 ) values(";
			sql += "?, ?, ?, ?, ?, ?, ?, sysdate, ?, ?, ?, ?)";
		}
		else
		{
			sql +=", CREATE_DATE, RATING, ORDER_NO, IMG1, ALT1, IMG2, ALT2, IMG3, ALT3, IMG4, ALT4) values(";
			sql += "?, ?, ?, ?, ?, ?, ?, sysdate, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		}
		
		
		try{
			pstmt = conn.prepareStatement(sql.toString());

			for(int i=0;i<Products.getLength();i++)
			{
				//System.out.println("#### : " + i);
				
				Node cur = Products.item(i);
				NamedNodeMap attrs =  cur.getAttributes();
				/*
				System.out.println("<" +cur.getNodeName() + ">" + NVL(cur.getNodeValue()));
				System.out.println("<attrs>");
				for(int j=0;j<attrs.getLength();j++)
				{
					Node attr = attrs.item(j);
					System.out.println("   [" +attr.getNodeName() + "]" + NVL(attr.getNodeValue()));
				}
				System.out.println("</attrs>");
				
				System.out.println("</" +cur.getNodeName() + ">");
				*/
				
				String[] imgNames = getImagePreviews(cur);
				
				Node nameNode = attrs.getNamedItem("Name");
				Node idNode = attrs.getNamedItem("ID");
				Node descNode = attrs.getNamedItem("Description");
				Node ratingNode = attrs.getNamedItem("Rating");
				
				String vendor = "";
				if("JAVA".equals(productType))
				{
					Node vendorNode = attrs.getNamedItem("Vendor");
					vendor = NVL(vendorNode.getNodeValue());
				}
				
				String name = NVL(nameNode.getNodeValue());
				String id = NVL(idNode.getNodeValue());
				String desc = NVL(descNode.getNodeValue());
				String rating = NVL(ratingNode.getNodeValue());
				

	            pstmt.setString( 1, SITE_CD);  //SITE_CD
	            pstmt.setString( 2, LANG_CD);  //LANG_CD
	            pstmt.setString( 3, id);  //PRODUCT_ID
	            pstmt.setString( 4, name);  //PRODUCT_NAME
	            pstmt.setString( 5, desc);  //PRODUCT_DESC
	            pstmt.setString( 6, productType);  //TYPE
	            pstmt.setString( 7, vendor);  //VENDOR
	            pstmt.setString( 8, rating);  //rating
	            pstmt.setString( 9, "999");  // Order_no
	            
	            if("RINGTONE_POLY".equals(productType) || "REALTONE".equals(productType))
            	{
	            	String imgName = imgNames[0];
	            	pstmt.setString( 10, imgName);  //IMG1, IMG2
	            	pstmt.setString( 11, "");  //IMG1, IMG2
            	}
            	else
            	{
            		int idx = 0;
		            for(int k=0;k<4;k++)
		            {
		            	String imgName = "";
		            	if(k>imgNames.length-1)
		            	{
		            		imgName="";
		            	}
		            	else
		            	{
		            		imgName = imgNames[k];
		            	}
		            	
		            	
			            pstmt.setString( idx + 10, imgName);  //IMG
			            idx++;
			            pstmt.setString( idx + 10, "");  //ALT
			            idx++;
		            }
            	}

	            //pstmt.executeBatch();
	            pstmt.addBatch();
	            
	            executeProductCategory(conn, productType, id, getOwnerCategory(cur));
	            
	            if("JAVA".equals(productType))
	            {
	            	executeJavaSupported(conn, id, getJavaSupported(cur));
	            }
	            cnt++;
			}
			pstmt.executeBatch();
			LogWriter.setLog("####### [" + productType + "] SUCCESS : " + cnt + " #########");

		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e;}
        }
		return sql;
	}
	
	public String executeProductCategory(Connection conn, String productType, String prdId, String[] cates) throws Exception
	{

		String sql 	= "insert into G_SFC_DCMS_CATE_INFO(SITE_CD, LANG_CD, CATE_ID, PRODUCT_ID, TYPE, CREATE_DATE) values(";
			   sql += "?, ?, ?, ?, ?, sysdate)";
		PreparedStatement pstmt=null;
		try{
			

			pstmt = conn.prepareStatement(sql.toString());
			
			for(int i=0;i<cates.length;i++)
			{
				String cateId = (String)cates[i];

	            pstmt.setString( 1, SITE_CD);  		//SITE_CD
	            pstmt.setString( 2, LANG_CD);  		//LANG_CD
	            pstmt.setString( 3, NVL(cates[i]));  //PRODUCT_ID
	            pstmt.setString( 4, prdId);  			//PRODUCT_NAME
	            pstmt.setString( 5, productType);  	//PRODUCT_DESC

	            pstmt.addBatch();
			}
			pstmt.executeBatch();
		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e;}
        }
		return sql;
	}
	
	public boolean executeJavaSupported(Connection conn, String prdId, String[] supportedPhones) throws Exception
	{

		String sql 	= "insert into G_SFC_DCMS_JAVA_SUPPORTED(SITE_CD, LANG_CD, PRODUCT_ID, PHONE_ID, CREATE_DATE) values(";
			   sql += "?, ?, ?, ?, sysdate)";
		PreparedStatement pstmt=null;
		try{
			

			pstmt = conn.prepareStatement(sql.toString());
			
			for(int i=0;i<supportedPhones.length;i++)
			{
				String cateId = (String)supportedPhones[i];

	            pstmt.setString( 1, SITE_CD);  		//SITE_CD
	            pstmt.setString( 2, LANG_CD);  		//LANG_CD
	            pstmt.setString( 3, prdId);  			//PRODUCT_ID
	            pstmt.setString( 4, NVL(supportedPhones[i]));  //PHONE_ID

	            pstmt.addBatch();
			}
			pstmt.executeBatch();
		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
            if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e;}
        }
		return true;
	}
	
	public boolean deletePopularPrd(Connection conn, String productType) throws Exception
	{
		String sql 	= "delete from g_sfc_dcms_popular where site_cd = ? and lang_cd = ? and product_type = ? and product_id not in ";
		sql += "(select product_id from g_sfc_dcms_product where site_cd = ? and lang_cd = ? and product_type = ?)";
		PreparedStatement pstmt=null;
		try{
			
			pstmt = conn.prepareStatement(sql.toString());
			
			pstmt.setString( 1, SITE_CD);  		//SITE_CD
            pstmt.setString( 2, LANG_CD);  		//LANG_CD
            pstmt.setString( 3, productType);  	//PRODUCT_ID
            pstmt.setString( 4, SITE_CD);  		//SITE_CD
            pstmt.setString( 5, LANG_CD);  		//LANG_CD
            pstmt.setString( 6, productType);  	//PRODUCT_ID
            
            pstmt.execute();
	
		}
		catch(Exception e)
		{
			throw e;
		}
		finally {
	     if (pstmt != null) try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); throw e;}
		}
		return true;
	}
	
	/*
	 * �x��� �� �̸���� ���� ��/url ��n�1�
	 */
	public String[] getImagePreviews(Node cur)
	{
		ArrayList imgNames = new ArrayList();
		
		Node child = cur.getFirstChild();
		while(child != null)
		{
			if("Previews".equals(child.getNodeName()))
			{
				Node imageNode = child.getFirstChild();
				while(imageNode!=null)
				{
					if("Preview".equals(imageNode.getNodeName()) || "ImagePreview".equals(imageNode.getNodeName()))
					{
						NamedNodeMap attrs =  imageNode.getAttributes();
						for(int j=0;j<attrs.getLength();j++)
						{
							Node attr = attrs.item(j);
							if("Url".equals(attr.getNodeName()))
							{
								imgNames.add(attr.getNodeValue());
							}
							else if("Height".equals(attr.getNodeName()))
							{
								//
							}
							else if("Width".equals(attr.getNodeName()))
							{
								//
							}
								
						}
					}
					imageNode = imageNode.getNextSibling();
				}
				
			}
			child = child.getNextSibling();
		}
		
		if(imgNames.size()==0)
		{
			imgNames.add("");
		}
		return (String[])imgNames.toArray(new String[imgNames.size()]);
	}
	
	/*
	 * �x��� �� �̸���� ���� ��/url ��n�1�
	 */
	public String[] getOwnerCategory(Node prd)
	{
		ArrayList cates = new ArrayList();
		
		Node cur = prd.getFirstChild();
		while(cur != null)
		{
			if(cur.getNodeType() == Node.ELEMENT_NODE && "Categories".equals(cur.getNodeName()))
			{
				Node cateNode = cur.getFirstChild();
				while(cateNode!=null)
				{
					if(cateNode.getNodeType() == Node.ELEMENT_NODE && "CategoryID".equals(cateNode.getNodeName()))
					{
						cates.add(cateNode.getTextContent());
					}
					cateNode = cateNode.getNextSibling();
				}
				
			}
			cur = cur.getNextSibling();
		}
		
		return (String[])cates.toArray(new String[cates.size()]);
	}
	
	/*
	 * PRODUCT_TYPE�� JAVA�ΰ͸� ��� SUPPORT PHONE ��n�1�
	 */
	public String[] getJavaSupported(Node prd)
	{
		ArrayList phones = new ArrayList();
		
		Node cur = prd.getFirstChild();
		while(cur != null)
		{
			if(cur.getNodeType() == Node.ELEMENT_NODE && "SupportedPhones".equals(cur.getNodeName()))
			{
				Node phoneNode = cur.getFirstChild();
				while(phoneNode!=null)
				{
					if(phoneNode.getNodeType() == Node.ELEMENT_NODE && "PhoneID".equals(phoneNode.getNodeName()))
					{
						phones.add(phoneNode.getTextContent());
					}
					phoneNode = phoneNode.getNextSibling();
				}
				
			}
			cur = cur.getNextSibling();
		}
		
		return (String[])phones.toArray(new String[phones.size()]);
	}
	
	

}
